package org.example.leetcode;
import java.util.*;
/**
 * @version 1.0.0
 * @author: lynn
 * @description: z字形打印字符串
 * @date: 2021/7/24 8:49
 */
public class LC6 {
    public static void main(String[] args) {
        LC6 lc=new LC6();
        System.out.println(lc.convert("hellofuture",3));
    }

    public String convert(String s, int numRows) {
        if(numRows>s.length()||numRows<=1) return s;
        List<StringBuilder> rows=new ArrayList();
        for(int i=0;i<numRows;i++) {
            rows.add(new StringBuilder()); //完成初始化
        }
        int curRow=0;
        Boolean goDown=false;
        char[] sChars=s.toCharArray();
        for(int i=0;i<sChars.length;i++){
            if (curRow==0 || curRow==numRows-1){
                goDown=!goDown;
            }
            rows.get(curRow).append(sChars[i]);
            curRow+=goDown?1:-1;
        }
        StringBuilder res=new StringBuilder();
        for (StringBuilder sb:rows){
            res.append(sb);
        }
        return res.toString();
    }

}
